<!--头部公共部分-->
{include file="common/header" /}

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>客户管理</a>
        <a>
          <cite>公海客户</cite></a>
      </span>
	<a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
	   href="javascript:location.replace(location.href);" title="刷新">
		<i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div id="wodekehu" class="x-body" class="clear:both;">
	<div class="layui-row"
		 style="padding-left:20px;margin: 10px 0 -10px 0;;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
		<div class="layui-form layui-col-md12 x-so layui-form-pane" style="margin-bottom: -3px;">
			<div class="layui-input-inline">
				<input type="text" style="width:300px;" name="keywords" placeholder="请输入搜索书店名称、联系电话" autocomplete="off" class="layui-input">
			</div>
			<div class="layui-input-inline">
				<button class="layui-btn" id="search">
					<i class="layui-icon layui-icon-search"></i>筛选</button>
				<button class="layui-btn layui-btn-danger" id="clear">
					<i class="layui-icon"></i>重置</button>
			</div>
			<button class="layui-btn layui-btn-sm layui-btn-primary layui-btn-radius" style="margin-left:50px;" id="receive">
				领取客户
			</button>

			<button data-method="offset" data-type="r" class="layui-btn layui-btn-sm layui-btn-primary layui-btn-radius"
					id="assignment">转给同事
			</button>
		</div>
	</div>
	<table class="layui-table" id="datalist" lay-filter="Event"></table>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
	layui.use(['element','table',  'upload', 'laydate', 'layer','form'], function () {
		let table = layui.table,
			element = layui.element,
			laydate = layui.laydate,
			form = layui.form,
			layer = layui.layer,
			$ = layui.$;

		//限定可选日期
		laydate.render({
			elem: '#date'
			,range: true
			,trigger: 'click'
		});

		let loading = layer.load();
		//数据表格 方法级渲染
			let tableIns = table.render({
				elem: '#datalist'
				, url: '{:url("Customer/public_index")}'
				, method: 'POST'
				, loading: true
				, parseData: function (res) { //res 即为原始返回的数据
					if (res.code === 1) {
						res.code = 0;
					} else {
						res.code = -1;
					}
					return {
						"code": res.code, //解析接口状态
						"msg": res.message, //解析提示文本
						"count": res.data.total, //解析数据长度
						"data": res.data.data //解析数据列表
					};
				},
				cols: [[
					{type: 'checkbox'},
					{field: 'name', title: '书店名称',style:'color:#01AAED;font-weight: bold;',width:200, align: 'center', event: 'setSign'},
					{field: 'address', title: '省市区',minWidth:150, align: 'center'},
					{field: 'phone', title: '联系电话',width:150, align: 'center'},
					{field: 'customer_name', title: '联系人',width:100, align: 'center'},
					{field: 'intentional_grade_text', title: '客户成熟度',width:120, align: 'center', sort: true},
					{field: 'next_follow_up_time', title: '下次联系时间',width:170, align: 'center', sort: true},
					{field: 'follow_up_time', title: '最后联系时间',width:170, align: 'center', sort: true},
					{field: 'register_date', title: '入库日期',width:120, align: 'center', sort: true},
					{field: 'course_consultant', title: '跟进人',width:100, align: 'center'},
				]]
				, page: true
				, limit: 10
				, done: function () {
					layer.close(loading);
				}
			});


		//领取客户
		$('#receive').click(function () {
			let checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值
			if (checkStatus.data.length == 0) {
				layer.msg('请至少选择一条数据', {icon: 7, time: 2000}, function () {
				});
				return false;
			}

			let checked_ids = [];
			for (let index = 0; index < checkStatus.data.length; index++) {
				const element = checkStatus.data[index];
				//该客户是系统或者无效则无法领取
				if (element.course_consultant_type == 1 || element.status == 2) {
					layer.msg('所选数据中已存在归属或无效数据,请重新选择', {icon: 7, time: 2000}, function () {
						tableIns.reload();
					});
					return false;
				}
				checked_ids.push(element.id)
			}


			let data = {"ids": checked_ids.join(',')};
			let url = '{:url("Customer/receive_customer")}';
			$.post(url,data,function(response){
				if(response.code === 1){
					layer.msg(response.msg, {icon: 1, time: response.wait * 1000},function () {
						tableIns.reload();
					});
				}
			});
		});

		//转给同事
		$('#assignment').click(function () {
			let checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值
			if (checkStatus.data.length == 0) {
				layer.msg('请至少选择一条数据', {icon: 7, time: 2000}, function () {
				});
				return false;
			}

			let lg = checkStatus.data.length;
			let checked_ids = [];
			for (let index = 0; index < checkStatus.data.length; index++) {
				const element = checkStatus.data[index];
				if (element.status == 2) {
					layer.msg('所选数据中已存在无效客户,请重新选择', {icon: 7, time: 2000}, function () {
						tableIns.reload();
					});
					return false;
				}
				checked_ids.push(element.id)
			}
			console.log(checked_ids);
			layer.open({
				type: 2,
				skin: 'layui-layer-lan',
				closeBtn: 2,
				title: '转给同事 (共'+lg+'条)',
				area: ['60%', '60%'],
				shadeClose: false,
				btnAlign: 'c',
				shade: 0,
				content: "{:url('Customer/assignment')}?ids=" + checked_ids,
			});
		});

		//监听单元格事件
		table.on('tool(Event)', function (obj) {
			let data = obj.data; //获得当前行数据
			let layEvent = obj.event; //获得 lay-event 对应的值
			if(layEvent === 'setSign'){
				layer.open({
					type: 2,
					skin: 'layui-layer-lan',
					closeBtn: 2,
					title: '客户详情',
					area: ['80%', '80%'],
					shadeClose: false,
					content: "{:url('Customer/information')}?id=" + data.id + "&status="+1
				});
			}
		});

		//搜索项
		$('#search').click(function () {
			let field = {};
			field.keywords = $('input[name=keywords]').val();
			tableIns.reload({
				where: field,
				method: 'POST',
				page: {
					curr: 1 //重新从第 1 页开始
				}
			});
		});
		//清除
		$('#clear').click(function () {
			$('.layui-input').each(function (index, element) {
				$(element).val('');
			})
			tableIns.reload({
				where: {
					'keywords': ''
				},
				page: {
					curr: 1 //重新从第 1 页开始
				}
			});
		});
	});
</script>